package com.gmall.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gmall.common.result.Result;
import com.gmall.model.product.BaseCategoryTrademark;
import com.gmall.model.product.BaseTrademark;
import com.gmall.model.product.dto.CategoryTrademarkDTO;
import com.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.gmall.product.mapper.BaseTrademarkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkService {
    /**
     * 查询分类下的品牌信息
     * @param category3Id
     * @return
     */

    @Autowired
    BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;

    public Result<List<BaseTrademark>> findTrademarkList(Long category3Id) {

        /**
         * SELECT * from base_category_trademark where category3_id = 192
         *
         * SELECT * from base_trademark where
         * id in( SELECT trade_mark_id from base_category_trademark where category3_id = 192 and is_deleted = 0)
         */

        //SELECT * from base_category_trademark where category3_id = 192 and is_deleted = 0
        //1、根据category3Id 查询关联的品牌
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks =
                baseCategoryTrademarkMapper.selectList(queryWrapper);

        //trade_mark_id
        //2、通过stream.map获取 分类id查询分类品牌关联表的品牌id
        List<Long> trademarkIds = baseCategoryTrademarks.stream()
                .map((BaseCategoryTrademark a) -> {
                    return a.getTrademarkId();
                }).collect(Collectors.toList());

        //SELECT * from base_trademark where
        //    id in( SELECT trade_mark_id from base_category_trademark where category3_id = 192 and is_deleted = 0)
        //3、根据Id列表查询出 品牌列表
        if(!CollectionUtils.isEmpty(trademarkIds)) {
            List<BaseTrademark> baseTrademarks = baseTrademarkMapper.selectBatchIds(trademarkIds);
            return Result.ok(baseTrademarks);
        }
        return Result.fail();
    }

    public Result<List<BaseTrademark>> findCurrentTrademarkList(Long category3Id) {

        //1、根据category3Id 查询关联的品牌
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks =
                baseCategoryTrademarkMapper.selectList(queryWrapper);
        //2、使用Stream.map 获取关联品牌的ID
        List<Long> alreadyExist = baseCategoryTrademarks.stream()
                .map((BaseCategoryTrademark baseCategoryTrademark) -> {
                    return baseCategoryTrademark.getTrademarkId();
                }).collect(Collectors.toList());
        //3、查询所有的品牌
        List<BaseTrademark> allBaseTrademarkList = baseTrademarkMapper.selectList(Wrappers.emptyWrapper());

        //4、从所有的品牌中过滤出不包含以上ID的品牌
        if(!CollectionUtils.isEmpty(alreadyExist))
        {
            List<BaseTrademark> notExist = allBaseTrademarkList.stream()
                    .filter((a) -> {
                        return !alreadyExist.contains(a.getId()) ? true : false;
                    }).collect(Collectors.toList());
            return Result.ok(notExist);
        }
        return Result.ok(allBaseTrademarkList);
    }

    public Result<String> removeBaseCategoryTradeMark(Long category3Id, Long trademarkId) {

        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        queryWrapper.lambda().eq(BaseCategoryTrademark::getTrademarkId, trademarkId);
        baseCategoryTrademarkMapper.delete(queryWrapper);
        return Result.ok();
    }

    /**
    * 保存三级分类和品牌中间表的数据
     *@param categoryTrademarkDTO
     * 一次性保存多条数据
     */
    public Result<String> saveBaseCategoryTradeMark(CategoryTrademarkDTO categoryTrademarkDTO) {
        Long category3Id = categoryTrademarkDTO.getCategory3Id();
        List<Long> trademarkIdList = categoryTrademarkDTO.getTrademarkIdList();

        trademarkIdList.stream().forEach((trademarkId)->{

            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            baseCategoryTrademark.setCategory3Id(category3Id);
            baseCategoryTrademark.setTrademarkId(trademarkId);
            baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
        });

        return Result.ok();
    }
}
